app.controller('RedCtrl', function(
    $scope,
    $state,
    $mydialog,
    $ionicPopup,
    $http,
    $myconfig,
    $ionicModal,
    $stateParams,
    PopupService,
    $rootScope,
    $ionicConfig) {

    $scope.redId = $stateParams.id;
    //地址
    var MyAddress;

    //获取当前地理坐标
    function getPosition() {

        var options = {
            enableHighAccuracy: true,
            maximumAge: 3600000
        };

        var watchID = navigator.geolocation.getCurrentPosition(onSuccess, onError, options);

        function onSuccess(position) {

            //调用百度地图api将当前获取坐标转换为详细地址
            var convertor = new BMap.Convertor();
            var pointArr = [];
            pointArr.push(new BMap.Point(position.coords.longitude, position.coords.latitude));
            convertor.translate(pointArr, 1, 5, function(data) {
                if (data.status === 0) {
                    var geoc = new BMap.Geocoder();
                    var point = new BMap.Point(data.points[0].lng, data.points[0].lat);
                    // _this.photo.point = point;
                    geoc.getLocation(point, function(rs) {
                        //获取地理位置经纬度
                        console.log(rs.point);
                        MyAddress = rs.addressComponents.city + rs.addressComponents.province + '市' + rs.addressComponents.district
                        localStorage.setItem('MyAddress', JSON.stringify(MyAddress));
                        // alert(localStorage.getItem('MyAddress'));
                    });
                }
            });
        }

        function onError(error) {
            // alert('code: '    + error.code    + '\n' +
            // 'message: ' + error.message + '\n');
        }
    }

    getPosition();


    $scope.open_share = function() {
        PopupService.popShare($scope);
    };


    $scope.pup = function() {
        $scope.fail_text = "失败提示";
        $mydialog.fail($scope);

    };

    //请求红包详情
    $scope.redMes = function(id) {
        $http.get($myconfig.api.base_host + '/redpacket/redpacket_info?redpacket_id=' + id)
            .then(function(res) {
                $scope.redMesData = res.data.data;
            });
    };



    function redSuccess(_if) { //成功提示
        if (_if) {
            $scope.IsIf = '恭喜您抢到' + (_if) + '元红包';
        } else if (!_if) {
            $scope.IsIf = _if;
        }
        // console.log(123);
        $scope.modal = $ionicModal.fromTemplate('<div class="red-pup"><p ng-if="!IsIf">有点小遗憾，没有抢到红包，</br>下次再接再厉~</p><p ng-if="IsIf">{{IsIf}}，</br>&nbsp;&nbsp;</p><span ng-click="toMylist()">去我的红包看看></span> <img class="img_btn" src="img/icons/btn_qiuhaoyun.svg" ng-click="open_share()">   <img src="img/loginicons/popup_hongbao2.png" alt="" ></div>', {
            scope: $scope,
            animation: 'slide-in-up'
        });
        $scope.modal.show();

    }

    //前往我的红包
    $scope.toMylist = function() {
        $scope.modal.hide();
        $rootScope.gored = true;
        $state.go('app.vip_red', { redid: $scope.redId });
    };

    //红包领取列表
    //分页请求预置限制
    $scope.redpackIf = {
        // hasmore_redpack : false,
        page: 1,
        limit: 10
    };

    //预置显示数据空数组
    $scope.redpacktData = [];
    //请求红包活动列表
    $scope.redPacket = function() {

        $http.get($myconfig.api.base_host + '/redpacketlogs/totle_list?page=' + $scope.redpackIf.page + '&limit=' + $scope.redpackIf.limit + '&redpacket_id=' + $scope.redId)
            .then(function(res) {
                // console.log(res.data.data);
                $scope.redpacktData = $scope.redpacktData.concat(res.data.data);
            });
    };

    $scope.redPacket();

    //抢红包
    // 定义红包服务器未返回是不得二次执行
    var isOK = true;
    $scope.redsu = function() {
        if(isOK){
            isOK = !isOK;
            $http.post($myconfig.api.base_host + '/redpacket/grab_redpacket', {
                "redpacket_id": $scope.redId,
                "address": MyAddress
            }).then(function(res) {
                isOK = true;
                if (res.data.success) {
                    redSuccess(res.data.data);
                    $http.get($myconfig.api.base_host + '/users/user_info?mobile=' + $rootScope._RootUser.mobile)
                        .then(function(res) {
                            if (res.data.success) {
                                localStorage.setItem('user', JSON.stringify(res.data.data))
                            }
                        });
                } else {
                    redSuccess(false);
                }
            });
        }else if(!isOK){
            console.log('抢红包动作不可同时执行多次！！！');
        }
    };
    if (!$scope.redId) {
        $scope.redId = $scope.redId;
        if ($scope.redId) {
            $scope.redMes($scope.redId);
        }
    } else if ($scope.redId) {
        $scope.redMes($scope.redId);
    }

    $scope.$on('$ionicView.enter', function() {

        $ionicConfig.views.swipeBackEnabled(false);

    });
});
